package com.cxq.finalproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.cxq.finalproject.databinding.ActivityUploadImgBinding;
import com.cxq.finalproject.util.GetImgIDByName;
import com.cxq.finalproject.util.MyClothHelper;

public class Upload_Img_Activity extends AppCompatActivity {
    private ActivityUploadImgBinding binding;
    private SharedPreferences sp;
    private MyClothHelper myClothHelper;
    private GetImgIDByName getImgID;
    private Boolean isFind = false;
    private String clothKind = "男装";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_img);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_upload_img);
        myClothHelper = new MyClothHelper(this,MyClothHelper.DB_NAME,null,1);

        //图片预览按钮监听
        binding.button10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(binding.editTextTextPersonName6.getText().toString().equals("")){
                    Toast.makeText(getApplicationContext(), "图片名不能为空",
                            Toast.LENGTH_SHORT).show();
                }else if(binding.editTextNumber.getText().toString().equals("")){
                    Toast.makeText(getApplicationContext(), "价格不能为空!",
                            Toast.LENGTH_SHORT).show();
                }else if(binding.editTextTextPersonName7.getText().toString().equals("")){
                    Toast.makeText(getApplicationContext(), "简介不能为空!",
                            Toast.LENGTH_SHORT).show();
                }else{
                    getImgID = new GetImgIDByName(binding.editTextTextPersonName6.getText().toString());
                    //如果找不到则返回-1
                    if(getImgID.getResId() == -1){
                        Toast.makeText(getApplicationContext(), "图片不到啊!",
                                Toast.LENGTH_SHORT).show();
                    }else{
                        binding.imageView4.setImageResource(getImgID.getResId());
                    }
                }
            }
        });

        //上传按钮监听
        binding.button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(binding.editTextTextPersonName6.getText().toString().equals("")){
                    Toast.makeText(getApplicationContext(), "图片名不能为空",
                            Toast.LENGTH_SHORT).show();
                }else if(binding.editTextNumber.getText().toString().equals("")){
                    Toast.makeText(getApplicationContext(), "价格不能为空!",
                            Toast.LENGTH_SHORT).show();
                }else if(binding.editTextTextPersonName7.getText().toString().equals("")){
                    Toast.makeText(getApplicationContext(), "简介不能为空!",
                            Toast.LENGTH_SHORT).show();
                }else{
                    getImgID = new GetImgIDByName(binding.editTextTextPersonName6.getText().toString());
                    //如果找不到则返回-1
                    if(getImgID.getResId() == -1){
                        Toast.makeText(getApplicationContext(), "图片不到啊!",
                                Toast.LENGTH_SHORT).show();
                    }else{
                        if( !isImgExist(binding.editTextTextPersonName6.getText().toString()) ){
                            SQLiteDatabase db = myClothHelper.getReadableDatabase();
                            ContentValues values = new ContentValues();
                            values.put(MyClothHelper.Name,binding.editTextTextPersonName6.getText().toString());
                            values.put(MyClothHelper.ClothClass,clothKind);
                            values.put(MyClothHelper.Introduce,binding.editTextTextPersonName7.getText().toString());
                            values.put(MyClothHelper.Isload,"0");
                            values.put(MyClothHelper.Price,binding.editTextNumber.getText().toString());
                            values.put(MyClothHelper.Sale,"0");
                            values.put(MyClothHelper.LoadUserId,"-1");
                            long rid  = db.insert(MyClothHelper.TABLE_NAME,MyClothHelper.Name,values);
                            db.close();
                            Toast.makeText(getApplicationContext(), "插入成功!",
                                    Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(getApplicationContext(), "图片已经存在!",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });

        //spin监听
        binding.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(i == 0){
                    clothKind = "男装";
                }else if(i == 1){
                    clothKind = "女装";
                }else{
                    clothKind = "童装";
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

    }
    public boolean isImgExist(String imgName){
        SQLiteDatabase db = myClothHelper.getReadableDatabase();
        Cursor c = db.query(MyClothHelper.TABLE_NAME, new String[]{MyClothHelper.Name,MyClothHelper.Price},
                MyClothHelper.Name + "=?", new String[] { imgName },
                null, null, null);
        Boolean result = false;

        if (c.getCount() > 0) {
            result = true;
        }
        db.close();
        return result;
    }
}